// eventBus.ts
import { ref, Ref } from 'vue';

interface EventBus {
  events: Ref<Record<string, Function[]>>;
  on(eventName: string, callback: (data: any) => void): void;
  off(eventName: string, callback: (data: any) => void): void;
  emit(eventName: string, data: any): void;
}

const eventBus: EventBus = {
  events: ref({} as Record<string, ((data: any) => void)[]>),

  on(eventName: string, callback: (data: any) => void) {
    if (!this.events.value[eventName]) {
      this.events.value[eventName] = [];
    }
    this.events.value[eventName].push(callback);
  },

  off(eventName: string, callback: (data: any) => void) {
    if (this.events.value[eventName]) {
      this.events.value[eventName] = this.events.value[eventName].filter(cb => cb !== callback);
    }
  },

  emit(eventName: string, data: any) {
    if (this.events.value[eventName]) {
      this.events.value[eventName].forEach(callback => callback(data));
    }
  }
};

export default eventBus;